from django.http import JsonResponse
from django.shortcuts import render, redirect
from py2neo import Graph

from db.import1 import *


# 按标签搜索，默认搜索
def showlist(request, way):
    list1 = get_top_videos_by_tag(way)
    x = '1'

    context = {
        'x': x,
        'list': list1,
        'way': '当前标签：' + way
    }
    return render(request, 'list.html', context)


# 按字段搜索up或视频
def search(request, piece):
    list, up_list = find_videos_by_keyword(piece)
    x = '1'
    if list == '1' and up_list == '1':
        x = '0'
    context = {
        'x': x,
        'list': list,
        'up_list': up_list,
        'way': '当前搜索字段：' + piece
    }
    return render(request, 'list.html', context)


# 进入视频详情页
def showdetail(request, video_name):
    # 获取视频信息
    this_video = get_video_info(video_name)
    total_command = get_related_videos(video_name)
    tag_anal = get_tags_sorted_by_avg_views(video_name)
    up_command = get_similar_ups(video_name)
    up_list = []
    for i in range(len(this_video['up_names'])):
        up_list.append({'up_name': this_video['up_names'][i], 'up_head': this_video['up_heads'][i]})
    context = {
        'video': this_video,
        'total_command': total_command,
        'up_command': up_command,
        'tag_anal': tag_anal,
        'up_list': up_list
    }
    return render(request, 'detail.html', context)


# 搜索某up主的视频
def upvideo(request, up_name):
    list2 = get_videos_by_up(up_name)
    x = '1'

    context = {
        'x': x,
        'list': list2,
        'way': '当前搜索up主：' + up_name
    }
    return render(request, 'list.html', context)
